//
//  PopViewUtil.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/5/12.
//

import UIKit
import AMPopTip
class PopViewUtil: NSObject {
    var popTip: PopTip!
    static let shared = PopViewUtil()
    func showPopView(_ text: String? = nil, textAttrString: NSAttributedString? = nil, customViewFlag: Bool = false , tipBtn: UIButton? = nil,tipLabel: UILabel? = nil,position: PopTipDirection = .down, popWidth: CGFloat = APPConfig.WidthForPortrait - 100,contentView: UIView? = nil){
        self.hidePopView()//每次显示前，先置空
        guard let rootVCView = Tool.getRootView() else {
            return
        }
        //背景颜色
        let backHexColor = "#6D6D6D"
        if popTip == nil {
            self.popTip = PopTip()
        }
        var rect: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
        if let tipView = tipBtn{
            if let parentView = contentView {
                rect = tipView.convert(tipView.bounds, to: parentView)//将tipBtn的坐标转换为在Window上的位置
            }else{
                rect = tipView.convert(tipView.bounds, to: rootVCView)//将tipBtn的坐标转换为在Window上的位置
            }
        }else if let tipView = tipLabel {
            if let parentView = contentView {
                rect = tipView.convert(tipView.bounds, to: parentView)//将tipBtn的坐标转换为在Window上的位置
            }else{
                rect = tipView.convert(tipView.bounds, to: rootVCView)//将tipBtn的坐标转换为在Window上的位置
            }
        }
        popTip.bubbleColor = UIColor(hexStr: backHexColor)
        popTip.textColor = UIColor.white
        popTip.font = R.font.sfProTextRegular(size: 13)!
        popTip.textAlignment = .left
        if let _ = text {
            if customViewFlag {//定制customeView
                let textView = UITextView()
                textView.text = text
                textView.font = R.font.sfProTextRegular(size: 13)!
                textView.textColor = UIColor.white
                textView.backgroundColor = UIColor(hexStr: backHexColor)
                let maxHeight: CGFloat = APPConfig.HeightForPortrait / 3
                let textViewWidth = APPConfig.WidthForPortrait - (rect.origin.x + rect.size.width) - 30
                let realSize = textView.sizeThatFits(CGSize(width: textViewWidth, height: CGFloat(MAXFLOAT)))
                if realSize.height > maxHeight {//大于最大高度
                    textView.isScrollEnabled = true
                    textView.showsVerticalScrollIndicator = true
                    textView.frame.size = CGSize(width: textViewWidth, height: maxHeight)
                }else{
                    textView.isScrollEnabled = false
                    textView.showsVerticalScrollIndicator = false
                    textView.frame.size = realSize
                }
                if let parentView = contentView {
                    popTip.show(customView: textView, direction: position, in: parentView, from: rect)
                }else{
                    popTip.show(customView: textView, direction: position, in: rootVCView, from: rect)
                }
            } else{
                if let parentView = contentView {
                    popTip.show(text: text!, direction: position, maxWidth: popWidth, in: parentView, from: rect)
                }else{
                    popTip.show(text: text!, direction: position, maxWidth: popWidth, in: rootVCView, from: rect)
                }
            }
        }else if let _ = textAttrString {
            popTip.show(attributedText: textAttrString!, direction: position, maxWidth: popWidth, in: rootVCView, from: rect)
        }
    }
    func hidePopView(){
        if let _ = self.popTip{
            popTip.hide(forced: true)//强制关闭
            popTip = nil
        }
    }
}
